﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

public class RangeDeviceView : MonoBehaviour
{
    public RangeDevice Device;

    public Button BtnPlayBack;
    public Button BtnStop;
    public Button BtnPlayForward;
    public Button BtnClear;

    public Text LabelName;
    public Text LabelMin;
    public Text LabelMax;
    public Text LabelValue;

    public Slider SliderRange;

    // Start is called before the first frame update
    void Start()
    {
        Assert.IsNotNull(Device);
        SliderRange.minValue = 0;
        SliderRange.maxValue = 1;
        LabelName.text = Device.Name;
        Binding();
    }

    void Update()
    {
        UpdateView();
    }

    void Binding()
    {
        BtnPlayBack.onClick.AddListener(() => {
            Device.PlayBack();
        });
        BtnStop.onClick.AddListener(() => {
            Device.Stop();
        });
        BtnPlayForward.onClick.AddListener(() => {
            Device.PlayForward();
        });
        BtnClear.onClick.AddListener(() => { 
            Device.Clear();
        });

        SliderRange.onValueChanged.AddListener((v) => {
            Device.SetNormalValue(v);
        });
    }

    void UpdateView()
    {
        LabelMin.text = Device.Min.ToString("F1");
        LabelMax.text = Device.Max.ToString("F1");
        SliderRange.SetValueWithoutNotify(Device.NormalValue);
        LabelValue.text = Device.Value.ToString("F1");
    }
}
